/**
 * 
 */
package Bai1;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * @author Nguyen Tuan Luc T127536
 *
 */
public class cHW05_TrafficPanel01_T127536 extends JPanel {
	
		
		
	int nNumberImg = 1;
	int iCurrentImg= 0;
	// khai bao duong dan cua img
	String[] sImgFileName = {
			".\\pics05\\SmallRedCar\\Car-08.gif",
			};
	// khai bao bien de doc image vao bo nho
	BufferedImage[] imgs = new BufferedImage[nNumberImg];
	Timer t;
	int x=-100,y=0, dx=5;
	
	public cHW05_TrafficPanel01_T127536(){
		
		try {
			// doc hinh anh luu vao bo nho (memory)
			for( int i=0; i<nNumberImg; i++){
				imgs[i]= ImageIO.read(new File(sImgFileName[i]));
			}
		} catch (IOException e) {
			// error off reading the file
			System.err.println("loading error");
		}
		
		t= new Timer(25, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				iCurrentImg++;
				if( iCurrentImg == nNumberImg){
					iCurrentImg=0;
				}
				x= x+ dx;
				if( x > getWidth()){
					x=-100;
				}
				
				repaint();
			}
		});
		
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		// display image
		
		g.drawImage(imgs[ iCurrentImg], x, y, this);
		//g.drawImage(imgs[1], 10, 50, this);	
	}
	
	public void start(){
		t.start();
	}
	
	public void stop(){
		t.stop();
	}
}
